import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'
import Load from 'components/load'

export default class ProjectVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  @observable test = ''

  // 判断有无项目
  @observable projectLists = false

  // 当前tab 索引
  @observable indexCurrent = window.sessionStorage.getItem('indexCurrent') || 0

  // 获取项目列表
  @observable prodectList = {}

  // 待确认项目列表
  @observable applyList = []

  // 洽谈中项目列表
  @observable qiatanList = []

  // 已拒绝项目列表
  @observable refuseList = []

  // 已确认项目列表
  @observable confirmList = []

  // 当前页面显示
  @observable currentView = []

  // 添加新的项目
  @action handleAddProject = () => {
    this.props.history.push('/addProject')
  }

  // 跳转到个人中心
  @action handleToUserCenter = () => {
    this.props.history.push('/userCenter')
  }

  // 跳转到项目详情页面
  @action handleToDetail = (id, tabs) => {
    this.props.history.push(`/projectDetail?${id}?${tabs}`)
  }

  // 点击tabs 事件
  @action handleTabs = async (tabs, index) => {
    switch (index) {
      case 0:
        this.indexCurrent = 0 // 已确认
        window.sessionStorage.setItem('indexCurrent', 0)
        this.currentView = this.confirmList
        break
      case 1:
        this.indexCurrent = 1 // 待确认
        window.sessionStorage.setItem('indexCurrent', 1)
        this.currentView = this.applyList
        break
      case 2:
        this.indexCurrent = 2 // 洽谈中
        window.sessionStorage.setItem('indexCurrent', 2)
        this.currentView = this.qiatanList
        break
      case 3:
        this.indexCurrent = 3 // 已拒绝
        window.sessionStorage.setItem('indexCurrent', 3)
        this.currentView = this.refuseList
        break
    }
  }

  @action PageInitialization = async () => {
    Load.loading(1, '加载中')
    const UserId = window.localStorage.getItem('UserId')
    let response = await post(api.WZPROJECTLIST, { WZId: UserId })
    if (response.Success) {
      Load.loading(2, '加载完成')
      setTimeout(() => {
        this.projectLists = true
      })
      this.prodectList = response.Value
      this.applyList = this.prodectList.ApplyList
      this.qiatanList = this.prodectList.QiatanList
      this.refuseList = this.prodectList.RefuseList
      this.confirmList = this.prodectList.ConfirmList
      if (window.sessionStorage.getItem('indexCurrent')) {
        switch (window.sessionStorage.getItem('indexCurrent')) {
          case '0':
            this.currentView = this.confirmList
            break
          case '1':
            this.currentView = this.applyList
            break
          case '2':
            this.currentView = this.qiatanList
            break
          case '3':
            this.currentView = this.refuseList
            break
        }
      } else {
        this.currentView = this.prodectList.ConfirmList
      }
    } else {
      Toast.fail(response.Message)
    }
  }
}
